package com.btashov.stringarray;

/**
 * @author Bahtiyar Tashov <btashov@gmail.com>
 */
//todo: Write a method to replaceJavaLang all spaces in a string with ‘%20’.
public class ReplaceCharString {

    public String replace(String input) {
        StringBuilder sb = new StringBuilder();
        char[] chars = input.toCharArray();
        for (int i = 0; i < input.length(); i++) {
            if (chars[i] == ' ') {
                sb.append("%20");
            } else {
                sb.append(chars[i]);
            }
        }
        return sb.toString();
    }

    public int spaceCount(String input) {
        char[] chars = input.toCharArray();
        int count = 0;
        for (int i = 0; i < input.length(); ++i) {
            if (chars[i] == ' ') {
                ++count;
            }
        }
        return count;
    }

    public int spaceCountNewFashion(String input) {
        char[] chars = input.toCharArray();
        int count = 0;
        for (char ch : chars) {
            if (ch == ' ') {
                ++count;
            }
        }
        return count;
    }

    public int countSpaceWithoutArray(String input) {
        int count = 0;
        for (int i = 0; i < input.length(); ++i) {
            if (input.charAt(i) == ' ') {
                ++count;
            }
        }
        return count;
    }

    //replaceJavaLang space instead of %20 in web url
    public String buildURL(String url) {
        StringBuilder sb = new StringBuilder();
        char[] UrlCharArray = url.toCharArray();
        for (int i = 0; i < UrlCharArray.length; i++) {
            if (UrlCharArray[i] == '%')
                sb.append(" ");
            else if (UrlCharArray[i] == '2' || UrlCharArray[i] == '0')
                sb.append("");
            else
                sb.append(UrlCharArray[i]);
        }
        return sb.toString();
    }

    public String replaceJavaLang(String input) {
        return input.replaceAll(" ", "%20");
    }
}
